import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  ManyToOne,
  JoinColumn,
} from "typeorm";
import { ManualTimestamp } from "../../../decorators/manual-timestamp.decorator";
import { CasinoDiceRound } from "./casino-dice-round.entity";
import { getBetTypeName } from "../enums/bet-type-mapping.enum";

export enum BetType {
  BIG = 1, // 大
  SMALL = 2, // 小
  ODD = 3, // 单
  EVEN = 4, // 双
  TRIPLE = 5, // 豹子
}

export enum CurrencyType {
  GOLD = 1, // 金币
  SILVER = 2, // 银币
}

export enum BetStatus {
  BETTING = 0, // 下注中
  DRAWN = 1, // 已开奖
  SETTLED = 2, // 已结算
}

@Entity("casino_dice_bets")
@ManualTimestamp()
export class CasinoDiceBet {
  @PrimaryGeneratedColumn()
  id!: number;

  @Column({ comment: "期数ID" })
  roundId!: number;

  @Column({ comment: "用户ID" })
  userId!: number;

  @Column({ comment: "角色ID" })
  characterId!: number;

  @Column({ type: "tinyint", comment: "下注类型" })
  betType!: BetType;

  @Column({ type: "bigint", comment: "下注金额" })
  betAmount!: number;

  @Column({ type: "tinyint", comment: "货币类型" })
  currencyType!: CurrencyType;

  @Column({ type: "tinyint", default: BetStatus.BETTING, comment: "状态" })
  status!: BetStatus;

  @Column({ type: "bigint", default: 0, comment: "中奖金额" })
  winAmount!: number;

  @Column("datetime", { name: "created_at", comment: "创建时间" })
  created_at!: Date;

  @Column("datetime", {
    name: "updated_at",
    nullable: true,
    comment: "更新时间",
  })
  updated_at!: Date | null;

  // 关联关系 - 移除外键约束，只保留字段引用
  // // @ManyToOne => CasinoDiceRound, (round) => round.diceBets)
  // // @JoinColumn
  // // round!: CasinoDiceRound;

  // 计算属性
  get isWinning(): boolean {
    return this.winAmount > 0;
  }

  get isLosing(): boolean {
    return this.status === BetStatus.SETTLED && this.winAmount === 0;
  }

  get profit(): number {
    return this.winAmount - this.betAmount;
  }

  get betTypeName(): string {
    return getBetTypeName(this.betType);
  }

  get currencyName(): string {
    return this.currencyType === CurrencyType.GOLD ? "金币" : "银币";
  }

  get statusName(): string {
    const names = {
      [BetStatus.BETTING]: "下注中",
      [BetStatus.DRAWN]: "已开奖",
      [BetStatus.SETTLED]: "已结算",
    };
    return names[this.status] || "未知";
  }
}
